﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using Com.Aote.ObjectTools;
using System.Json;
using Com.Aote.Behaviors;
using Com.Aote.Controls;

namespace Com.Aote.Pages
{
    public partial class 补气 : UserControl
    {
        public 补气()
        {
            InitializeComponent();
        }

        //输入气量计算阶梯信息
        private void ui_pregas_LostFocus(object sender, RoutedEventArgs e)
        {
            ui_chargeBusy.IsBusy = true;
            string userid = f_userid.Text;
            string pregas = ui_pregas.Text;
            if (userid.Equals(""))
            {
                MessageBox.Show("请先读卡！");
                ui_chargeBusy.IsBusy = false;
                return;
            }
            GeneralObject go = (GeneralObject)(from r in loader.Res where r.Name.Equals("user") select r).First();
            if (go.GetPropertyValue("f_jllx").ToString().Equals("金额"))
            {
                ui_chargeBusy.IsBusy = false;
                return;
            }
            else if (pregas.Equals(""))
            {
                //MessageBox.Show("请输入预购气量！");
                ui_chargeBusy.IsBusy = false;
                return;
            }
            WebClientInfo wci = (WebClientInfo)Application.Current.Resources["chargeserver"];
            string str = wci.BaseAddress + "/num/" + userid + "/" + pregas + "?uuid=" + System.Guid.NewGuid().ToString();
            Uri uri = new Uri(str);
            WebClient client = new WebClient();
            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadStringAsync(uri);
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            ui_chargeBusy.IsBusy = false;
            if (e.Error == null)
            {
                JsonObject items = JsonValue.Parse(e.Result) as JsonObject;
                ui_preamount.Text = Math.Round(double.Parse(items["f_chargenum"].ToString()), 2).ToString();
            }
            else
            {
                ui_chargeBusy.IsBusy = false;
                MessageBox.Show(e.Error.Message);
            }
        }

        //输入金额计算阶梯信息
        private void ui_preamount_LostFocus(object sender, RoutedEventArgs e)
        {
            ui_chargeBusy.IsBusy = true;
            string preamount = ui_preamount.Text;
            if (f_userid.Text.Equals(""))
            {
                MessageBox.Show("请先读卡！");
                ui_chargeBusy.IsBusy = false;
                return;
            }
            GeneralObject go = (GeneralObject)(from r in loader.Res where r.Name.Equals("user") select r).First();
            if (go.GetPropertyValue("f_jllx").ToString().Equals("气量"))
            {
                ui_chargeBusy.IsBusy = false;
                return;
            }
            else if (preamount.Equals(""))
            {
                //MessageBox.Show("请输入预购金额！");
                ui_chargeBusy.IsBusy = false;
                return;
            }
            String userid = f_userid.Text;
            WebClientInfo wci = (WebClientInfo)Application.Current.Resources["chargeserver"];
            string str = wci.BaseAddress + "/fee/" + userid + "/" + preamount + "?uuid=" + System.Guid.NewGuid().ToString();
            Uri uri = new Uri(str);
            WebClient client1 = new WebClient();
            client1.DownloadStringCompleted += client1_DownloadStringCompleted;
            client1.DownloadStringAsync(uri);
        }

        private void client1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            ui_chargeBusy.IsBusy = false;
            if (e.Error == null)
            {
                JsonObject items = JsonValue.Parse(e.Result) as JsonObject;
                ui_pregas.Text = Math.Round(double.Parse(items["chargeamont"].ToString()), 2).ToString();
            }
            else
            {
                ui_chargeBusy.IsBusy = false;
                MessageBox.Show(e.Error.Message);
            }
        }
    }
}